<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="区域">
            <el-select v-model="form.regionId" placeholder="請選擇區域" style="width: 370px;" @change="regionSel">
              <el-option
                v-for="item in regionList"
                :key="item.id"
                :label="item.nameCn"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="联系人">
            <el-input v-model="form.contact" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否默认">
            <el-radio v-for="item in dict.true_false" :key="item.id" v-model="form.defaultStatus" :label="item.dictKey">
              {{ item.dictValue }}
            </el-radio>
          </el-form-item>
          <el-form-item label="联系方式">
            <el-input v-model="form.phone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="中文地址">
            <el-input v-model="form.addressCn" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="英文地址">
            <el-input v-model="form.addressEn" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="regionId" label="区域"      >
          <template slot-scope="scope">
            {{ scope.row.regionName }}
          </template>
        </el-table-column>
        <el-table-column prop="contact" label="联系人"       />
        <el-table-column prop="defaultStatus" label="是否默认"      >
          <template slot-scope="scope">
            {{ dict.label.true_false[scope.row.defaultStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="联系方式"       />
        <el-table-column prop="addressCn" label="中文地址"       />
        <el-table-column prop="addressEn" label="英文地址"       />
        <el-table-column
          v-if="checkPer(['admin','selfAddress:edit','selfAddress:del'])"
          label="操作"
          width="150px"

        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSelfAddress from '@/api/sms/selfAddress'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getAll } from '@/api/oms/orderRegion'

const defaultForm = {
  addressCn: null,
  addressEn: null,
  contact: null,
  defaultStatus: false,
  phone: null,
  regionId: null,
  regionName: null
}
export default {
  name: 'SelfAddress',
  components: { pagination, crudOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['true_false'],
  cruds() {
    return CRUD({ title: '自取地址', url: 'selfAddress/page', sort: 'id:desc', crudMethod: { ...crudSelfAddress }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'selfAddress:add'],
        edit: ['admin', 'selfAddress:edit'],
        del: ['admin', 'selfAddress:del']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'contact', display_name: '联系人' }
      ],
      regionList: []
    }
  },
  created() {
    this.getRegionId()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getRegionId() {
      getAll({ parentId: 0 }).then(res => {
        if (res.code === 0) {
          this.regionList = res.data
        }
      })
    },
    regionSel(sel) {
      this.regionList.forEach(item => { if (item.id === sel) this.form.regionName = item.nameCn })
    }
  }
}
</script>

<style scoped>

</style>
